//
//  LoginViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/22.
//  Copyright © 2017年 song ce. All rights reserved.
//

/*
 PingFangSC-Ultralight
 2017-04-01 13:4:               PingFangSC-Regular
 2017-04-01 1:                        PingFangSC-Semibold
 W[4329:99] Font name:            PingFangSC-Thin
 2017-04-01ame:                      PingFangSC-Light
 2017-04GWFont name:       PingFangSC-Medium
 
 */

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import <Hyphenate/Hyphenate.h>
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollV;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    self.vcName = @"疯狂购物城";
    [super viewDidLoad];
    [self initRightBarItem];
    self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.pwdTextField.secureTextEntry = YES;
    if(USERPHONE.length){
        self.phoneTextField.text = USERPHONE;
    }else{
        NSString *lastUser =   [[NSUserDefaults standardUserDefaults]objectForKey:@"lastUser"];
        if(lastUser.length){
            self.phoneTextField.text = lastUser;
        }
    }
}

- (void)initRightBarItem{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame=CGRectMake(0, 0, 70, 40);
      [rightButton setTitle:@"注册" forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightButton setTitleColor:DEF_COLOR  forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    self.rightItem =rightButton;
}

-(void)rightAction{
    RegisterViewController *registerVC = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:registerVC animated:YES];
}

-(void)loginViewDismiss{
    if(self.isNotNeedBackBtn){
    }else{
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (IBAction)login:(UIButton *)sender {
    NSString *phone = self.phoneTextField.text;
    if(!phone.length){
        [MBProgressHUD showTipMessageInWindow:@"请输入手机号码"];
        return;
    }
    NSString *passWord = self.pwdTextField.text;
    if(!passWord.length){
        [MBProgressHUD showTipMessageInWindow:@"请输入密码"];
        return;
    }
    //2.25登录接口
    NSDictionary *paraDic = @{
                              @"phone":phone,
                              @"passWord":passWord,
                              @"userType":@"0"
                              };
   NSString *  ephone = [NSString stringWithFormat:@"%@0",phone];
  [MBProgressHUD showActivityMessageInWindow:@""];
    [Tools AFNetworkingRequestTypePostWithUrl:LOGIN paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"登录参数--%@",paraDic);
        DLog(@"登录--%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSDictionary *dataDic = resultDic[@"data"];
            if([dataDic isKindOfClass:[NSNull class]]){
                [MBProgressHUD dismiss];
                return ;
            };
            if([dataDic[@"status"] isEqualToString:@"true"]){
              
                //登录环信
                [[EMClient sharedClient] loginWithUsername:ephone
                                                  password:passWord
                                                completion:^(NSString *aUsername, EMError *aError) {
                                                    if (!aError) {
                                                        // 设置自动登录
                                                        [[EMClient sharedClient].options setIsAutoLogin:YES];
                                                        DLog(@"登陆环信成功");
                                                        [MBProgressHUD dismiss];
                                                        [MBProgressHUD showSuccessMessage:@"登录成功"];
                                                        NSUserDefaults *useDefault = [NSUserDefaults standardUserDefaults];
                                                        [useDefault setObject:dataDic[@"uid"] forKey:@"userId"];
                                                        [useDefault setObject:phone forKey:@"lastUser"];
                                                        [useDefault synchronize];
                                                        [AppDelegate appDealegate].uid =dataDic[@"uid"];
                                                        [AppDelegate appDealegate].userPhone = phone;
                                                        [[NSNotificationCenter defaultCenter]postNotificationName:@"loginSuccess" object:nil];
                                                         [self performSelector:@selector(loginViewDismiss) withObject:nil afterDelay:1];
                                                    } else {
                                                        [MBProgressHUD dismiss];
                                                         [MBProgressHUD showTipMessageInWindow:@"服务器繁忙，请稍后再试"];
                                                        DLog(@"登陆环信失败--%@",aError.errorDescription);
                                                    }
                                                }];
            
          }else{
                [MBProgressHUD dismiss];
                if(dataDic[@"errorString"]){
                    [MBProgressHUD showTipMessageInWindow:dataDic[@"errorString"]];
                }
            }
        }else{
            [MBProgressHUD dismiss];
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}


//忘记密码
- (IBAction)forgetPwd:(UIButton *)sender {
}
//qq登录
- (IBAction)qqLogin:(UIButton *)sender {
}
//微信登录
- (IBAction)wechatLogin:(UIButton *)sender {
}
//微博登录
- (IBAction)weiboLogin:(UIButton *)sender {
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
